package com.taotao.web.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.common.pojo.EasyUIResult;
import com.taotao.common.service.HttpClientService;
import com.taotao.manage.pojo.Content;

@Service
public class IndexService {

	@Autowired
	private HttpClientService httpClientService;

	private static final ObjectMapper MAPPER = new ObjectMapper();
	
	@Value("${TAOTAO_MANAGE_CONTENT}")
	private String TAOTAO_MANAGE_CONTENT;

	public String queryBigADs() {
		try {
			// 调用后台提供的api接口
			EasyUIResult<Content> easyUIResult = this.httpClientService.doGet(
					StringUtils.replace(TAOTAO_MANAGE_CONTENT, "{1}", "32"),
					new TypeReference<EasyUIResult<Content>>() {
					});
			List<Map<String, Object>> result = new ArrayList<>();
			// 解析rows字段内容
			for (Content content : easyUIResult.getRows()) {
				Map<String, Object> map = new HashMap<>();
				map.put("srcB", content.getPic());
				map.put("height", 240);
				map.put("alt", content.getTitleDesc());
				map.put("width", 670);
				map.put("src", content.getPic());
				map.put("widthB", 550);
				map.put("href", content.getUrl());
				map.put("heightB", 240);
				result.add(map);
			}
			// 返回一个json类型的字符串
			return MAPPER.writeValueAsString(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
